package com.hand.demo.api.controller.v1;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.hand.demo.app.service.HhrTaCertService;
import com.hand.demo.domain.entity.HhrTaCert;
import com.hand.demo.domain.repository.HhrTaCertRepository;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 证书表(HhrTaCert)表控制层
 *
 * @author
 * @since 2025-06-16 12:16:54
 */

@RestController("hhrTaCertController.v1")
@RequestMapping("/v1/{organizationId}/hhr-ta-certs")
public class HhrTaCertController extends BaseController {

    @Autowired
    private HhrTaCertRepository hhrTaCertRepository;

    @Autowired
    private HhrTaCertService hhrTaCertService;

    @ApiOperation(value = "证书表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<HhrTaCert>> list(HhrTaCert hhrTaCert, @PathVariable Long organizationId,
                                                @ApiIgnore @SortDefault(value = HhrTaCert.FIELD_ID,
                                                        direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<HhrTaCert> list = hhrTaCertService.selectList(pageRequest, hhrTaCert);
        return Results.success(list);
    }

    @ApiOperation(value = "证书表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}/detail")
    public ResponseEntity<HhrTaCert> detail(@PathVariable Long id) {
        HhrTaCert hhrTaCert = hhrTaCertRepository.selectByPrimary(id);
        return Results.success(hhrTaCert);
    }

    @ApiOperation(value = "创建或更新证书表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<List<HhrTaCert>> save(@PathVariable Long organizationId, @RequestBody List<HhrTaCert> hhrTaCerts) {
        validObject(hhrTaCerts);
        SecurityTokenHelper.validTokenIgnoreInsert(hhrTaCerts);
        hhrTaCerts.forEach(item -> item.setTenantId(organizationId));
        hhrTaCertService.saveData(hhrTaCerts);
        return Results.success(hhrTaCerts);
    }

    @ApiOperation(value = "删除证书表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody List<HhrTaCert> hhrTaCerts) {
        SecurityTokenHelper.validToken(hhrTaCerts);
        hhrTaCertRepository.batchDeleteByPrimaryKey(hhrTaCerts);
        return Results.success();
    }

}

